<template>
  <div>
    <el-card class="box-card" style="margin: 10px 0px">
      <div slot="header" class="clearfix">
        <!-- v-model="activeName" @tab-click="handleClick"> -->
        <!--头部 左侧 -->
        <el-tabs class="tab" v-model="activeName">
          <el-tab-pane label="销售额" name="sale"></el-tab-pane>
          <el-tab-pane label="访问量" name="visite"></el-tab-pane>
        </el-tabs>
        <!-- 头部右侧 -->
        <div class="right">
          <span @click="setDay">今日</span>
          <span @click="setWeek">本周</span>
          <span @click="setMonth">本月</span>
          <span @click="setYear">本年</span>
          <el-date-picker
            v-model="date"
            class="date"
            size="mini"
            type="datetimerange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </div>
      </div>
      <div>
        <el-row :gutter="10">
          <el-col :span="18">
            <div class="chart" ref="charts"></div>
          </el-col>
          <el-col :span="6" class="right">
            <h3>门店{{ title }}额排名</h3>
            <ul>
              <li>
                <span class="rindex">1</span>
                <span>肯德基</span>
                <span class="rvalue">263,564</span>
              </li>
              <li>
                <span class="rindex">2</span>
                <span>麦当劳</span>
                <span class="rvalue">244,574</span>
              </li>
              <li>
                <span class="rindex">3</span>
                <span>海底捞</span>
                <span class="rvalue">193,554</span>
              </li>
              <li>
                <span class="rect">4</span>
                <span>汉堡王</span>
                <span class="rvalue">293,894</span>
              </li>
              <li>
                <span class="rect">5</span>
                <span>真功夫</span>
                <span class="rvalue">263,564</span>
              </li>
              <li>
                <span class="rect">6</span>
                <span>芭比馒头</span>
                <span class="rvalue">133,64</span>
              </li>
              <li>
                <span class="rect">7</span>
                <span>麻辣烫</span>
                <span class="rvalue">53,564</span>
              </li>
            </ul>
          </el-col>
        </el-row>
      </div>
    </el-card>
  </div>
</template>

<script>
import echarts from 'echarts'
import dayjs from 'dayjs'
import { mapState } from 'vuex'
export default {
  name: '',
  data() {
    return {
      activeName: 'sale',
      mycharts: null,
      date: [], //收集时间数据
    }
  },
  mounted() {
    this.mycharts = echarts.init(this.$refs.charts)
    this.mycharts.setOption({
      title: {
        text: this.title + '趋势',
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow',
        },
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true,
      },
      xAxis: [
        {
          type: 'category',
          data: [],
          //  ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: 'value',
        },
      ],
      series: [
        {
          name: 'Direct',
          type: 'bar',
          barWidth: '60%',
          data: [],
          //  [10, 52, 200, 334, 390, 330, 220, 98, 167, 144, 77, 45],
          color: 'blue',
        },
      ],
    })
  },
  watch: {
    title() {
      this.mycharts.setOption({
        title: {
          text: this.title + '趋势',
        },
        xAxis: {
          data: this.title == '销售额' ? this.listState.orderFullYearAxis : this.listState.userFullYearAxis,
        },
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data: this.title == '销售额' ? this.listState.orderFullYear : this.listState.userFullYear,
            color: 'yellowgreen',
          },
        ],
      })
    },
    listState() {
      this.mycharts.setOption({
        title: {
          text: this.title + '趋势',
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow',
          },
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },
        xAxis: [
          {
            type: 'category',
            data: this.listState.orderFullYearAxis,
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        yAxis: [
          {
            type: 'value',
          },
        ],
        series: [
          {
            name: 'Direct',
            type: 'bar',
            barWidth: '60%',
            data: this.listState.orderFullYear,
            color: 'yellowgreen',
          },
        ],
      })
    },
  },
  methods: {
    setDay() {
      const day = dayjs().format('YYYY-MM-DD')
      this.date = [day, day]
    },
    setWeek() {
      const start = dayjs().day(1).format('YYYY-MM-DD')
      const end = dayjs().day(7).format('YYYY-MM-DD')
      this.date = [start, end]
    },
    setMonth() {
      const start = dayjs().startOf('month').format('YYYY-MM-DD')
      const end = dayjs().endOf('month').format('YYYY-MM-DD')
      this.date = [start, end]
    },
    setYear() {
      const start = dayjs().startOf('year').format('YYYY-MM-DD')
      const end = dayjs().endOf('year').format('YYYY-MM-DD')
      this.date = [start, end]
    },
  },
  //计算属性 标题
  computed: {
    title() {
      return this.activeName == 'sale' ? '销售额' : '访问量'
    },
    ...mapState({
      listState: (state) => state.home.list,
    }),
  },
}
</script>

<style scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}
.tab {
  width: 100%;
}
.right {
  position: absolute;
  right: 0;
}
.date {
  width: 200px;
  margin: 0 20px;
}
.right span {
  margin: 0 10px;
}
.chart {
  width: 100%;
  height: 300px;
}
ul {
  list-style: none;
  height: 300px;
  width: 100%;
  padding: 0;
}
li {
  height: 8%;
  margin: 10px 0;
}
.rindex {
  float: left;
  display: inline-block;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  background-color: #000;
  color: #fff;
  text-align: center;
}
.rvalue {
  float: right;
}
.rect {
  float: left;
  display: inline-block;
  width: 15px;
  height: 15px;
  border-radius: 50%;
  text-align: center;
}
</style>
